package huawei.support.v4.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import java.util.Locale;
import java.util.Map;
import o.ds;
import o.fpk;
import o.PagerAdapter;

public class HwViewPager extends ViewPager {
    private boolean circleFix;
    @NonNull
    private final Map<OnPageChangeListener, HwOnPagerChangeLister> pagerListener = new ds(1);
    @Nullable
    private DataSetObserver mOberserve;

    public static class RtlSavedState implements Parcelable {
        public static final ClassLoaderCreator<RtlSavedState> CREATOR = new ClassLoaderCreator<RtlSavedState>() {
            public /* synthetic */ Object createFromParcel(Parcel parcel) {
                return ˊ(parcel);
            }

            public /* synthetic */ Object createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return ˎ(parcel, classLoader);
            }

            public /* synthetic */ Object[] newArray(int i) {
                return ˎ(i);
            }

            public RtlSavedState ˎ(Parcel parcel, ClassLoader classLoader) {
                return new RtlSavedState(parcel, classLoader);
            }

            public RtlSavedState ˊ(Parcel parcel) {
                return new RtlSavedState(parcel, null);
            }

            public RtlSavedState[] ˎ(int i) {
                return new RtlSavedState[i];
            }
        };
        boolean ˊ;
        int ˏ;
        Parcelable ॱ;

        public RtlSavedState(Parcelable parcelable, int i, boolean z) {
            this.ॱ = parcelable;
            this.ˏ = i;
            this.ˊ = z;
        }

        RtlSavedState(Parcel parcel, ClassLoader classLoader) {
            if (classLoader == null) {
                classLoader = getClass().getClassLoader();
            }
            this.ॱ = parcel.readParcelable(classLoader);
            this.ˏ = parcel.readInt();
            this.ˊ = parcel.readByte() != (byte) 0;
        }

        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeParcelable(this.ॱ, i);
            parcel.writeInt(this.ˏ);
            parcel.writeByte(this.ˊ ? (byte) 1 : (byte) 0);
        }

        public int describeContents() {
            return 0;
        }
    }

    static class HWDataSetOberver extends DataSetObserver {
        @NonNull
        private final FixHWAdapter ˋ;

        private HWDataSetOberver(@NonNull FixHWAdapter dVar) {
            this.ˋ = dVar;
        }

        public void onChanged() {
            super.onChanged();
            this.ˋ.ॱ();
        }
    }

    class FixHWAdapter extends fpk {
        private int mRealCount;
        final  HwViewPager viewPager;

        FixHWAdapter(HwViewPager hwViewPager, @NonNull PagerAdapter mRealAdapter) {
            this.viewPager = hwViewPager;
            super(mRealAdapter);
            this.mRealCount = mRealAdapter.getCount();
        }

        public CharSequence getPageTitle(int i) {
            return super.getPageTitle(remainCount(i));
        }

        public float getPageWidth(int i) {
            return super.getPageWidth(remainCount(i));
        }

        public int getItemPosition(Object obj) {
            int position = super.getItemPosition(obj);
            return position < 0 ? position : remainCount(position);
        }

        public Object instantiateItem(ViewGroup viewGroup, int i) {
            return super.instantiateItem(viewGroup, remainCount(i));
        }

        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            super.destroyItem(viewGroup, remainCount(i), obj);
        }

        public void setPrimaryItem(ViewGroup viewGroup, int i, Object obj) {
            super.setPrimaryItem(viewGroup, (this.mRealCount - i) - 1, obj);
        }

        private int remainCount(int i) {
            return (getCount() - i) - 1;
        }

        private void ॱ() {
            int count = getCount();
            if (count != this.mRealCount) {
                this.viewPager.setCircleIndex(this.mRealCount + -1 > 0 ? this.mRealCount - 1 : 0);
                this.mRealCount = count;
            }
        }
    }

    class HwOnPagerChangeLister implements OnPageChangeListener {
        @NonNull
        private final OnPageChangeListener mchangeListener;
        private int mPostion;
        final  HwViewPager mPager;

        private HwOnPagerChangeLister(HwViewPager hwViewPager, @NonNull OnPageChangeListener aVar) {
            this.mPager = hwViewPager;
            this.mchangeListener = aVar;
            this.mPostion = -1;
        }

        public void onPageScrolled(int position, float offset, int offsetPixels) {
            if (!this.mPager.circleFix) {
                if (offset == 0.0f && offsetPixels == 0) {
                    this.mPostion = remainIndex(position);
                } else {
                    this.mPostion = remainIndex(position + 1);
                }
                OnPageChangeListener listener = this.mchangeListener;
                int i3 = this.mPostion;
                if (offset > 0.0f) {
                    offset = 1.0f - offset;
                }
                listener.onPageScrolled(i3, offset, offsetPixels);
            }
        }

        public void onPageScrollStateChanged(int i) {
            if (!this.mPager.circleFix) {
                this.mchangeListener.onPageScrollStateChanged(remainIndex(i));
            }
        }

        public void onPageScrolled(int i) {
            if (!this.mPager.circleFix) {
                this.mchangeListener.onPageScrolled(i);
            }
        }

        private int remainIndex(int index) {
            PagerAdapter adapter = this.mPager.getAdapter();
            return adapter == null ? index : (adapter.getCount() - index) - 1;
        }
    }

    public HwViewPager(Context context) {
        super(context);
    }

    public HwViewPager(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public void setAdapter(@Nullable PagerAdapter adapter) {
        unRegisterObserve();
        int initSelect = (adapter == null || !isCircleCountryAdapter()) ? 0 : 1;
        if (initSelect != 0) {
            PagerAdapter FixHWAdapter = new FixHWAdapter(this, adapter);
            registerObserver(FixHWAdapter);
            adapter = FixHWAdapter;
        }
        super.setAdapter(adapter);
        if (initSelect != 0) {
            setCircleIndex(0);
        }
    }

    @Nullable
    public PagerAdapter getAdapter() {
        PagerAdapter adapter = super.getAdapter();
        return adapter instanceof FixHWAdapter ? ((FixHWAdapter) adapter).getPageTitle() : adapter;
    }

    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(getRemainCount(item), smoothScroll);
    }

    public void setCurrentItem(int i) {
        super.setCurrentItem(getRemainCount(i));
    }

    public int getCurrentItem() {
        return getRemainCount(super.getCurrentItem());
    }

    public void addOnPageChangeListener(@NonNull OnPageChangeListener aVar) {
        if (isCircleCountryAdapter()) {
            HwOnPagerChangeLister eVar = new HwOnPagerChangeLister(aVar);
            this.pagerListener.put(aVar, eVar);
            aVar = eVar;
        }
        super.addOnPageChangeListener(aVar);
    }

    public void setOnPageChangeListener(OnPageChangeListener listener) {
        if (isCircleCountryAdapter()) {
            listener = new HwOnPagerChangeLister(listener);
        }
        super.setOnPageChangeListener(listener);
    }

    public void removeOnPageChangeListener(@NonNull OnPageChangeListener aVar) {
        OnPageChangeListener aVar2;
        if (isCircleCountryAdapter()) {
            aVar2 = (OnPageChangeListener) this.pagerListener.remove(aVar);
        } else {
            aVar2 = aVar;
        }
        super.removeOnPageChangeListener(aVar2);
    }

    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        registerObserver(super.getAdapter());
    }

    public void onDetachedFromWindow() {
        unRegisterObserve();
        super.onDetachedFromWindow();
    }

    public Parcelable onSaveInstanceState() {
        return new RtlSavedState(super.onSaveInstanceState(), getCurrentItem(), isCircleCountryAdapter());
    }

    public void onRestoreInstanceState(Parcelable parcelable) {
        RtlSavedState rtlSavedState = (RtlSavedState) parcelable;
        super.onRestoreInstanceState(rtlSavedState.ॱ);
        if (rtlSavedState.ˊ != isCircleCountryAdapter()) {
            setCurrentItem(rtlSavedState.ˏ, false);
        }
    }

    private void registerObserver(PagerAdapter ozVar) {
        if ((ozVar instanceof FixHWAdapter) && this.mOberserve == null) {
            this.mOberserve = new HWDataSetOberver((FixHWAdapter) ozVar);
            ozVar.registerDataSetObserver(this.mOberserve);
            ((FixHWAdapter) ozVar).ॱ();
        }
    }

    private void unRegisterObserve() {
        PagerAdapter adapter = super.getAdapter();
        if ((adapter instanceof FixHWAdapter) && this.mOberserve != null) {
            adapter.unregisterDataSetObserver(this.mOberserve);
            this.mOberserve = null;
        }
    }

    private int getRemainCount(int i) {
        if (i < 0 || !isCircleCountryAdapter()) {
            return i;
        }
        return getAdapter() == null ? 0 : (getAdapter().getCount() - i) - 1;
    }

    private void setCircleIndex(int index) {
        this.circleFix = true;
        setCurrentItem(index, false);
        this.circleFix = false;
    }

    private boolean isCircleCountryAdapter() {
        String language = Locale.getDefault().getLanguage();
        boolean z = language.contains("ar") || language.contains("fa") || language.contains("iw");
        boolean z2;
        if (language.contains("ug") || language.contains("ur")) {
            z2 = true;
        } else {
            z2 = false;
        }
        if (z || z2) {
            return true;
        }
        return false;
    }
}
